package com.edroid.apkhooker.home;

import java.util.LinkedHashMap;
import java.util.Map;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.edroid.apkhooker.R;
import com.edroid.autorunner.AutoRunActivity;
import com.edroid.autorunner.SettingsDialog;
import com.edroid.autorunner.StateFloatButton;
import com.example.clone.CloneActivity;
import com.example.utils.Shell;
import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitlePageIndicator.IndicatorStyle;
import com.viewpagerindicator.TitlePageIndicator.OnCenterItemClickListener;

public class MainActivity extends BaseActivity implements
		OnCenterItemClickListener {
	ViewPager mPager;
	TitlePageIndicator mIndicator;
	RecentAppListFragment recentAppList;
	AppListFragment appListFragment;
	boolean showSysApps; // 显示系统应用

	@Override
	public void onBackPressed() {
		// 屏蔽默认的finish()动作
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(new MyFragmentPagerAdapter(
				getSupportFragmentManager()));

		mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);
		mIndicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
		mIndicator.setOnCenterItemClickListener(this);
		mIndicator.setCurrentItem(0);

		mIndicator.setTextColor(0x80336699);
		mIndicator.setSelectedColor(0xf0336699);
		mIndicator.setFooterColor(0xf0336699);

		getSupportActionBar().setSubtitle("未运行");

		recentAppList = new RecentAppListFragment();
		appListFragment = new AppListFragment();

		StateFloatButton.getInstance().onActivityCreate(this);
	}

	@Override
	protected void onResume() {

		System.out.println("onResume");

		getSupportActionBar().setSubtitle(
				AutoRunActivity.curRunningAppName == null ? "未运行"
						: AutoRunActivity.curRunningAppName);

		super.onResume();
	}

	@Override
	protected void onPause() {

		System.out.println("onPause");

		super.onPause();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 7, 2, "刷新应用列表");

		MenuItem item = menu.add(0, 2, 1, "系统应用");
		item.setCheckable(true);
		item.setChecked(showSysApps);

		menu.add(0, 5, 2, "克隆");
		menu.add(0, 1, 2, "清理数据库事务");

		menu.add(0, 3, 2, "参数设置");
		menu.add(0, 4, 2, "系统设置");
		menu.add(0, 6, 2, "Holo桌面");
		menu.add(0, 8, 2, "多选");
		// 下个ID 8
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 2: {
			showSysApps = !showSysApps;
			item.setChecked(showSysApps);
			appListFragment.setShowSysApps(showSysApps);
			break;
		}

		case 1: {
			new Thread() {
				@Override
				public void run() {
					Shell.runRootCmd("rm /sdcard/apkhooker/dbs/*.db-journal");
					MainActivity.this.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							Toast.makeText(MainActivity.this, "清理完成",
									Toast.LENGTH_LONG).show();
						}
					});
				}
			}.start();
			break;
		}

		case 3: {
			SettingsDialog.show(getFragmentManager());
			break;
		}
		case 4: {
			// Intent intent = new Intent();
			// ComponentName comp = new ComponentName("com.android.settings",
			// "com.android.settings.Settings");
			// intent.setComponent(comp);
			// intent.setAction("android.intent.action.VIEW");
			// startActivityForResult(intent, 0);
			Intent intent = new Intent(
					android.provider.Settings.ACTION_SETTINGS); // 系统设置
			startActivityForResult(intent, 0);
			break;
		}
		case 5: {// 克隆
			Map<String, String> map = new LinkedHashMap<String, String>();
			map.put("更新apkhooker", "");
			CloneActivity.setData(map);
			startActivity(new Intent(this, CloneActivity.class));
			break;
		}
		case 6: {// Holo桌面
			try {
				Intent intent = getActivity().getPackageManager()
						.getLaunchIntentForPackage("com.mobint.hololauncher");
				startActivity(intent);
			} catch (Exception e) {
				try {
					Intent intent = new Intent();
					intent.setComponent(new ComponentName("com.android.launcher","com.android.launcher2.Launcher"));
					startActivity(intent);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
			break;
		}
		case 7: {// 刷新应用列表
			appListFragment.mLoader.onContentChanged();
			break;
		}
		default:
			return super.onOptionsItemSelected(item);
		}

		return true;
	}

	static final String[] PAGE_TITLES = { "最近打开", "所有应用", "工具箱" };

	public final class MyFragmentPagerAdapter extends FragmentPagerAdapter
			implements IconPagerAdapter {

		public MyFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return recentAppList;
			case 1:
				return appListFragment;
			case 2:
				return new FunctionFragment();
			}

			return null;
		}

		@Override
		public int getCount() {
			return PAGE_TITLES.length;
		}

		@Override
		public int getIconResId(int index) {
			return 0;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return PAGE_TITLES[position % PAGE_TITLES.length];
		}
	}

	MainActivity getActivity() {
		return this;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onCenterItemClick(int position) {
	}

	public void addToRecent(String pkg) {
		recentAppList.add(pkg);
	}

}
